package com.lightkits.wms.domain.mainpage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.db.entity.*;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.mainpage.service.ItemInfoByMoDomainService;
import com.lightkits.wms.dto.mainpage.model.MoItemInfoDTO;
import org.springframework.stereotype.Service;
/**
 * ItemInfoByMoDomainServiceImpl
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-21 12:47
 */
@Service
public class ItemInfoByMoDomainServiceImpl implements ItemInfoByMoDomainService {

    @Override
    public MoItemInfoDTO getItemInfoByMoNum(String moNum) {
        MoItemInfoDTO dto = new MoItemInfoDTO();
        BdMoBase moBase = new BdMoBase().selectOne(new EntityWrapper().eq(BdMoBase.MO_NUM,moNum));

        if (!ObjectUtil.isEmpty(moBase)) {
            FndItemBase itemBase = new FndItemBase().selectOne(new EntityWrapper().eq(FndItemBase.ITEM_ID,moBase.getItemId()));

            FndItemTl itemTl = new FndItemTl().selectOne(new EntityWrapper().eq(FndItemTl.ITEM_ID,moBase.getItemId()));

            FndUomBase uomBase = new FndUomBase().selectOne(new EntityWrapper().eq(FndUomBase.UOM_CODE,itemBase.getPrimaryUom()));

            FndUomTl uomTl = new FndUomTl().selectOne(new EntityWrapper().eq(FndUomTl.UOM_ID,uomBase.getId()));

            if (!ObjectUtil.isEmpty(itemBase) && !ObjectUtil.isEmpty(itemTl)){

                if (!ObjectUtil.isEmpty(itemBase.getSecondaryUom())) {
                    FndUomBase uomBase2 = new FndUomBase().selectOne(new EntityWrapper().eq(FndUomBase.UOM_CODE,itemBase.getSecondaryUom()));
                    FndUomTl uomTl2 = new FndUomTl().selectOne(new EntityWrapper().eq(FndUomTl.UOM_ID,uomBase2.getId()));
                    dto.setSecondaryUomText(uomTl2.getUomName());
                }
                dto.setItemId(ObjectUtil.toString(itemBase.getItemId()));
                dto.setItemCode(itemBase.getItemCode());
                dto.setPrimaryUom(itemBase.getPrimaryUom());
                dto.setPrimaryUomText(uomTl.getUomName());
                dto.setSecondaryUom(itemBase.getSecondaryUom());
                dto.setDescription(itemTl.getDescription());
            }
        }
        return dto;
    }
}
